#include "qtranslatewidget.h"

#include "qtranslatetreewidget.h"
#include "qaddtranslatedialog.h"

#include "../../libs/coreLib/styledbar.h"
#include "../../libs/coreLib/qactiontoolbar.h"
#include "../../libs/coreLib/qsoftcore.h"
#include "../../libs/sharedLib/qprojectcore.h"
#include "../../libs/sharedLib/qlanguagemanager.h"

#include <QHeaderView>
#include <QVBoxLayout>
#include <QLabel>
#include <QAction>

QTranslateWidget::QTranslateWidget(QWidget *parent) :
    CORELIB::QAbstractPageWidget(parent),
    m_translateTree(new QTranslateTreeWidget(this)),
    m_styledBar(new CORELIB::StyledBar(this))
{
    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);

    v->addWidget(m_styledBar);
    v->addWidget(m_translateTree);

    this->setLayout(v);

    QStringList list;
    list<<tr("ID");
    m_translateTree->setHeaderLabels(list);
    m_translateTree->setAlternatingRowColors(true);
    m_translateTree->setEditTriggers(QAbstractItemView::EditKeyPressed);
    m_translateTree->setRootIsDecorated(false);
    m_translateTree->header()->setMovable(false);
    //m_translateTree->setItemDelegate(new QUserDelegate);
    m_translateTree->setColumnWidth(0,200);
    m_translateTree->setFrameStyle(QFrame::NoFrame);

    CORELIB::QActionToolBar *toolbar=new CORELIB::QActionToolBar(Qt::AlignRight,this);

    v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);
    v->addWidget(toolbar);
    m_styledBar->setLayout(v);



    SHAREDLIB::QProjectCore *c=SHAREDLIB::QProjectCore::getCore();
    connect(c,SIGNAL(projectOpen()),this,SLOT(projectOpen()));
    connect(c,SIGNAL(projectClosed()),this,SLOT(projectClosed()));
    connect(c->languageManager(),SIGNAL(refresh()),this,SLOT(refreshLanguage()));

    QLabel *l=new QLabel;
    l->setText(tr("Translate Manager"));
    l->setStyleSheet("color:rgb(255,255,255);");

    m_languageComboBox=new QComboBox;
    m_languageComboBox->setMinimumSize(100,20);
    QWidget *wid=new QWidget;
    QHBoxLayout *h=new QHBoxLayout;
    h->setMargin(0);
    h->setSpacing(0);
    h->addWidget(l,1);
    h->addWidget(m_languageComboBox,0);
    wid->setLayout(h);
    toolbar->setLabelWidget(wid);

    /*QList<QAction*> aclist;
    QAction* ac;

    CORELIB::QSoftCore *core=CORELIB::QSoftCore::getCore();

    ac=core->action("TranslateManager-Add");
    if(ac!=NULL)
    {
        aclist.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(addTranslate()));
    }

    ac=core->action("TranslateManager-Remove");
    if(ac!=NULL)
    {
        aclist.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(removeTranslate()));
    }

    ac=new QAction(this);
    ac->setSeparator(true);
    aclist.append(ac);

    toolbar->addButtonActions(aclist);*/
}

void QTranslateWidget::addTranslate()
{
    QAddTranslateDialog dlg(this);

    dlg.exec();
}

void QTranslateWidget::removeTranslate()
{
    ((QTranslateTreeWidget*)m_translateTree)->removeCurrent();
}

void QTranslateWidget::projectOpen()
{
    SHAREDLIB::QProjectCore *c=SHAREDLIB::QProjectCore::getCore();
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
    m_languageComboBox->clear();
    m_languageComboBox->addItems(c->languageManager()->languages());
    QObject* obj=c->languageManager()->getCurrentLanguage();
    if(obj!=NULL)
    {
        m_languageComboBox->setCurrentIndex(
                    m_languageComboBox->findText(
                        obj->property("languageName").toString()));
    }
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
}

void QTranslateWidget::refreshLanguage()
{
    SHAREDLIB::QProjectCore *c=SHAREDLIB::QProjectCore::getCore();
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
    m_languageComboBox->clear();
    m_languageComboBox->addItems(c->languageManager()->languages());
    QObject* obj=c->languageManager()->getCurrentLanguage();
    if(obj!=NULL)
    {
        m_languageComboBox->setCurrentIndex(
                    m_languageComboBox->findText(
                        obj->property("languageName").toString()));
    }
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
}

void QTranslateWidget::projectClosed()
{
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    m_languageComboBox->clear();
    ((QTranslateTreeWidget*)m_translateTree)->clearAll();
}

void QTranslateWidget::changedLanguage(const QString &lan)
{
    SHAREDLIB::QProjectCore *c=SHAREDLIB::QProjectCore::getCore();
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
    c->languageManager()->setCurrentLanguage(lan);
    ((QTranslateTreeWidget*)m_translateTree)->updateAll();
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
}

void QTranslateWidget::currentLanguageChanged()
{
    SHAREDLIB::QProjectCore *c=SHAREDLIB::QProjectCore::getCore();
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
    QObject* obj=c->languageManager()->getCurrentLanguage();
    if(obj!=NULL)
    {
        m_languageComboBox->setCurrentIndex(
                    m_languageComboBox->findText(
                        obj->property("languageName").toString()));
    }
    ((QTranslateTreeWidget*)m_translateTree)->updateAll();
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(changedLanguage(QString)));
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(currentLanguageChanged()));
}
